<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="commodityName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          style="width: 180px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" prop="commodityTypeName">
        <el-input
          v-model="queryParams.commodityTypeName"
          placeholder="请输入商品类型名称"
          style="width: 180px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品价格范围" label-width="98px">
        <el-input
          v-model="queryParams.minPrice"
          prop="minPrice"
          placeholder="请输入商品最低价"
          style="width: 150px"
          clearable
          @change="checkPriceRange"
          @keyup.enter.native="handleQuery"
        />
        -
        <el-input
          v-model="queryParams.maxPrice"
          prop="maxPrice"
          placeholder="请输入商品最高价"
          style="width: 150px"
          clearable
          @change="checkPriceRange"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link to="/createCommodity/index">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
        >新增
        </el-button>
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commodityList">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="table-expand">
            <el-form-item label="商品ID">
              <span>{{ props.row.commodityId }}</span>
            </el-form-item>
            <el-form-item label="商品名称">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="库存">
              <span>{{ props.row.stock }}</span>
            </el-form-item>
            <el-form-item label="市场价">
              <span>{{ props.row.marketPrice }}</span>
            </el-form-item>
            <el-form-item label="成本价">
              <span>{{ props.row.costPrice }}</span>
            </el-form-item>
            <el-form-item label="商品分类">
              <span>{{ props.row.commodityTypeName }}</span>
            </el-form-item>
            <el-form-item label="虚拟销量">
              <span>{{ props.row.virtualSales }}</span>
            </el-form-item>
            <el-form-item label="商品关键字">
              <span>{{ props.row.keywords }}</span>
            </el-form-item>
            <el-form-item label="商品描述">
              <span>{{ props.row.description }}</span>
            </el-form-item>
            <el-form-item label="商品单位">
              <span>{{ props.row.unit }}</span>
            </el-form-item>
            <el-form-item label="商品积分">
              <span>{{ props.row.score }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品编号" align="center" prop="commodityId"/>
      <el-table-column label="商品名称" align="center" prop="name"/>
      <el-table-column label="商品类型" align="center" prop="commodityTypeName"/>
      <el-table-column label="商品售价" align="center" prop="price">
        <template slot-scope="scope">
          <span>{{scope.row.price / 100}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>

</template>


<script>
import { delCommodity, listCommodity } from '../../../api/commodity/commodity'
import router from '../../../router'

export default {
  name: 'Commodity',
  dicts: [],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      commodityList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commodityName: undefined,
        commodityTypeName: undefined,
        minPrice: undefined,
        maxPrice: undefined
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: '岗位名称不能为空', trigger: 'blur' }
        ],
        postCode: [
          { required: true, message: '岗位编码不能为空', trigger: 'blur' }
        ],
        postSort: [
          { required: true, message: '岗位顺序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    //商品价格
    checkPriceRange() {
      this.isInvalid = this.queryParams.minPrice !== null && this.queryParams.maxPrice !== null && this.queryParams.minPrice > this.queryParams.maxPrice

      if (this.isInvalid) {
        this.$message.error('最低价不能大于最高价')
      }
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true
      listCommodity(this.queryParams).then(response => {
        this.commodityList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.queryParams.minPrice > this.queryParams.maxPrice) {
        this.$message.error('最低价不能大于最高价')
        return // 阻止搜索操作
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        commodityName: undefined,
        commodityTypeName: undefined,
        minPrice: undefined,
        maxPrice: undefined
      }
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加岗位'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const commodityId = row.commodityId;
      this.$router.push({ name: 'update', params: { id: commodityId } });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commodityId = row.commodityId
      this.$modal.confirm('是否确认删除商品编号为"' + commodityId + '"的数据项？').then(function() {
        return delCommodity(commodityId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('commodity/commodity/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style>
.table-expand {
  font-size: 0;
}

.table-expand label {
  width: 90px;
  color: #99a9bf;
}

.table-expand .el-form-item {
  align: center;
  margin-left: 40px;
  margin-right: 0;
  margin-bottom: 0;
  width: 30%;
}
</style>
